<?php
/** ******************************************************************************
 * 
 * This file is under copyright of Zeedler Ztudio. Do not be modified or amended,
 * To study or co-source code. Can be contacted at administrator@zeedler.net.
 * Or visit the information. www.zeedler.com and www.zeedler.net.
 * 
 ********************************************************************************/ 

class Ztools {
	/**
	 * Check install. Return Boolean.
	 * If return true because your project are installatoin.
	 * else if return false because your project is not install.
	 * This function it will check the lock.txt if in your path folder set
	 * have lock.txt it will report install successfull and return true.
	 */
	function checkinstall($path) {
		$strpos = strpos($path, "/", strlen($path)-1);
		if (false === $strpos)
			$strpos = strpos($path, "\\", strlen($path)-1);
		if (false !== $strpos) { $path .= "/"; }
		$path .= "lock.txt";
		if (file_exists($path)) { return true; }
		else { return false; }
	}
	
	/**
	 * Root Path. Return String.
	 * If you want to know the root path use this function.
	 */
	function rootPath() {
		$root = $_SERVER["DOCUMENT_ROOT"];
		$dir = dirname(__FILE__);
		$dir = str_replace($root, "", $dir);
		$rootPath = $root.$dir."/";
		return $rootPath;
	}
	
	/**
	 * Connect to Database. Return Boolean.
	 * if it can connect to DataBase it return the connection variable.
	 * if return false. it can't connect to database, please check your variables.
	 * If it return 2 Because you must create the DataBase first.
	 */
	function connectdb($dbhost, $dbuser, $dbpass, $dbname, $charset = "utf8") {
		$connect = @mysql_connect($dbhost, $dbuser, $dbpass);
		if (mysql_error()) { return false; }
		elseif ((!$dbname) && (!mysql_error())) { return $connect; }
		if ($dbname) {
			$selectdb = @mysql_select_db($dbname);
			if (mysql_error()) { return 2; }
			else {
				$query[0] = "SET character_set_client = $charset";
				$query[1] = "SET character_set_connection = $charset";
				$query[2] = "SET character_set_database = $charset";
				$query[3] = "SET character_set_results = $charset";
				$query[4] = "SET character_set_server = $charset";
				for ($i=0; $i<=4; $i++) { @mysql_query($query[$i]); }
				return $connect;
			}
		}
	}
	
	/**
	 * Keygen for Random string. Return String.
	 * $minlength,$maxlength = min and max length string.
	 * $useupper = use upcase character.
	 * $usenumbers = use number character.
	 * $usespecial = use special character.
	 */
	function keygen($minlength, $maxlength, $useupper, $usenumbers, $usespecial) {
	    $charset = "abcdefghijklmnopqrstuvwxyz";
	    if ($useupper) { $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
	    if ($usenumbers) { $charset .= "0123456789"; }
	    if ($usespecial) {
	    	if ($usespecial == 1) { $charset .= "~@#\$%^*()_+-={}|]["; }
			elseif ($usespecial > 1) { $charset .= "~!@#\$%^&*()_+`-={}|\\]?[\":;'><,./"; }
	    }
	    if ($minlength > $maxlength) { $length = mt_rand($maxlength, $minlength); }
	    else { $length = mt_rand($minlength, $maxlength); }
	    for ($i = 0; $i < $length; $i++) { $key .= $charset[(mt_rand(0, (strlen($charset) - 1)))]; }
	    return $key;
	}
	
	/**
	 * Apply in English only. Return Boolean And Status Bool.
	 * if return true or 1 because your string is true all of character in set.
	 * else return 2 because length of string are invalid.
	 * else return 3 beacuse character of your string are invalid.
	 * $string = your string for check character.
	 * $minlength, $maxlenght = min and max length of your string.
	 * $whitespace = use whitespace. $useupper = use upcase character.
	 * $usenumbers = use number for check.
	 * $usespecial = use special character for check.
	 */
	function fixengonly($string, $minlength, $maxlength, $whitespace, $useupper, $usenumbers, $usespecial) {
		$charset = "[a-z";
		if ($whitespace) { $charset .= " "; }
		if ($useupper) { $charset .= "A-Z"; }
		if ($usenumbers) { $charset .= "0-9"; }
		if ($usespecial) { $charset .= "@#\$%&*)(_+-=|':;><.,"; }
	    $charset .= "]";
	    $invalid = true;
	    $string = trim($string);
	    if ((strlen($string) < $minlength) || (strlen($string) > $maxlength) || (!$string)) {
	    	$invalid = 2; #if return this because length of string are invalid.
	    } else {
	    	$lenstring = strlen($string);
	    	$exit = false;
	    	while (false === $exit) {
	    		if ($i <= $lenstring) {
	    			$compare = substr($string, $i, 1);
	    			if (ereg($charset, $compare)) { $i++; } else { $exit = true; $invalid = 3; }
	    			#if return this because character of your string are invalid.
	    		}
				if ($i == $lenstring){ $exit = true; }
	    	}
	    }
	    return $invalid;
	}
	
	/**
	 * Get User IP-Address. Return String IP.
	 * if you use secret option it return xxx in position of enable.
	 * Default of secret position is 4.
	 */
	function userIP($secret, $position1 = 0, $position2 = 0, $position3 = 0, $position4 = 1) {
		$ip = $_SERVER['REMOTE_ADDR'];
		if ($secret) {
			$ip = str_replace(".", " ", $ip);
			$nip = split(" ", $ip);
			if ($position1) { $nip[0] = "xxx"; }
			if ($position2) { $nip[1] = "xxx"; }
			if ($position3) { $nip[2] = "xxx"; }
			if ($position4) { $nip[3] = "xxx"; }
			$ip = $nip[0].".".$nip[1].".".$nip[2].".".$nip[3];
		}
		return $ip;
	}
	
	/**
	 * Check Language for user. Return 2 String of ISO 639-1 codes.
	 * function check your variables ?lang, cookie lang or HTTP_ACCEPT_LANGUAGE.
	 * The check with ACP Lang file in langs folder.
	 * if it have in set it will return lang. but it don't have
	 * it will return default language in array[0].
	 */
	function getlang($acplangfile) {
		$lang = $_GET['lang'];
		if (!$lang) {
			$lang = $_COOKIE['lang'];
			if (!$lang) {
				$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
			}
		}
		require_once($acplangfile);
		$use = false;
		for ($i=0; $i<=count($accept_lang); $i++) {
			if ($accept_lang[$i] == $lang) { $use = true; }
		}
		
		if (false === $use) { $lang = $accept_lang[0]; }
		if (!$_COOKIE["lang"]) { @setcookie("lang", $lang, time()+3600*24*356, "/"); }
		return $lang;
	}
	
	/**
	 * Cleans the request from script injection.
	 * if you want to clean all varibles use this function.
	 */
	function cleanVar() {
		Ztools::_cleanArray( $_FILES );		Ztools::_cleanArray( $_ENV );		Ztools::_cleanArray( $_GET );
		Ztools::_cleanArray( $_POST );		Ztools::_cleanArray( $_COOKIE );	Ztools::_cleanArray( $_SERVER );
		if (isset( $_SESSION )) { tools::_cleanArray( $_SESSION ); }
		$REQUEST	= $_REQUEST;		$GET		= $_GET;		$POST		= $_POST;
		$COOKIE		= $_COOKIE;			$FILES		= $_FILES;		$ENV		= $_ENV;
		$SERVER		= $_SERVER;
		if (isset ( $_SESSION )) { $SESSION = $_SESSION; }
		foreach ($GLOBALS as $key => $value) {
			if ( $key != 'GLOBALS' ) { unset ( $GLOBALS [ $key ] ); }
		}
		$_REQUEST	= $REQUEST;		$_GET		= $GET;		$_POST		= $POST;
		$_COOKIE	= $COOKIE;		$_FILES		= $FILES;	$_ENV		= $ENV;
		$_SERVER	= $SERVER;
		if (isset ( $SESSION )) { $_SESSION = $SESSION; }
	}
	
	/**
	 * Clean PHP variables.
	 * $array are $_FILES, $_ENV, $_GET, $_POST, $_COOKIE, $_SERVER, $_SESSION and $_REQUEST.
	 */
	function _cleanArray( &$array, $globalise=false ) {
		static $banned = array( '_files', '_env', '_get', '_post', '_cookie', '_server', '_session', 'globals' );
		foreach ($array as $key => $value) {
			// PHP GLOBALS injection bug
			$failed = in_array( strtolower( $key ), $banned );
			// PHP Zend_Hash_Del_Key_Or_Index bug
			$failed |= is_numeric( $key );
			if ($failed) { exit(); }
			if ($globalise) { $GLOBALS[$key] = $value; }
		}
	}
	
	/**
	 * Mysql Query. Return Query Mysql.
	 * if it return false because your sql command is invalid.
	 * $sql = SQL command (SQL Language).
	 * if you want return set $return = 1, but you don't want set 0.
	 * Default $return = 1.
	 */
	function mysql_query($sql, $return = 1) {
		if ($sql) {
			$query = @mysql_query($sql);
			if (mysql_error()) { return false; }
			elseif ($return != 0) { return $query; }
		}
	}
	
	/**
	 * Mysql Result. Return Array Data.
	 * if return false beacuse your sql command is invalid.
	 * $query = query of mysql.
	 * $result = fetch_array, num_rows, affected_rows and fetch_object.
	 * $result Default = fetch_array.
	 */
	function mysql_result($query, $result = "fetch_array") {
		if (($result) && ($query)) {
			switch ($result) {
				case "fetch_array" : $data = @mysql_fetch_array($query); break;
				case "num_rows" : $data = @mysql_num_rows($query); break;
				case "affected_rows" : $data = @mysql_affected_rows($query); break;
				case "fetch_object" : $data = @mysql_fetch_object($query); break;
				default : $data = @mysql_fetch_array($query); break;
			}
			if (mysql_error()) { return false; }
			else {
				return $data;
				mysql_free_result($data);
			}
		}
	}
	
	/**
	 * File Replace. Return Boolean. or Result of Replace.
	 * $path = Path of file of you want to replace.
	 * $replace = String of replace. You can replace multiple string
	 * if you use "<>" between the string.
	 * example : "\$config = 1 -| \$default = 1 <> default_path -| mydefault_path";
	 *
	 * Method String of find and String of replace.
	 * example : findThisString -| replaceThisString
	 *
	 * If you want to replace and you don't know what are string.
	 * you can use this |*| symbol.
	 * example : "config = (|*|); -| localhost";
	 * result : config = (localhost)
	 * warn****** : before and after |*| symbol must have string.
	 *
	 * You can edit your variable and print your string varaible on file.
	 * set : $config = 1;
	 * example : "\$config = $config";
	 * result : $config = 1
	 *
	 * if it return false because your path file is invalid.
	 * if you want to return the result of replace you can set
	 * $result = 1
	 * example of return : "Replace : $config = 1; TO $config = 2;"
	 */
	function file_replace($path, $replace, $result = 0) {
		if ($path) {
			$fp = @fopen($path, "r");
			if ($fp) {
				while (!feof($fp)) { $string .= fgets($fp); }
				fclose($fp);
				if ($replace) {
					$rep = explode("<>", $replace);
					$c = count($rep) - 1;
					$replace_counter = 0;
					for ($i=0; $i<=$c; $i++) {
						$star_pos = false;
						$rep[$i] = trim($rep[$i]);
						$rep[$i] = explode("-|", $rep[$i]);
						$rep[$i][0] = trim($rep[$i][0]);
						$rep[$i][1] = trim($rep[$i][1]);
						
						$star_pos = strpos($rep[0][0], "|*|");
						if (false !== $star_pos) {
							$sub1 = substr($rep[$i][0], 0, $star_pos);
							$sub2 = substr($rep[$i][0], $star_pos+3, strlen($rep[$i][0])-($str_pos+3));
							$exit = false;
							$int = 0;
							$sk = 0;
							while(false === $exit) {
								$pos1 = strpos($string, $sub1, $int);
								$int = $pos1+strlen($sub1);
								$pos2 = strpos($string, $sub2, $int);
								$int = $pos2+strlen($sub2);
								if ((false !== $pos1) && (false !== $pos2)) {
									$rex[0] = substr($string, 0, $pos1+strlen($sub1));
									$rex[1] = substr($string, $pos2, strlen($string)-($rex[0]+$rex[1]));
									$string= $rex[0].$rep[$i][1].$rex[1];
									if ($result != 0) {
										$replace_counter++;
										if (strlen($rex[0]) > 15) { $rex[0] = substr($rex[0], strlen($rex[0])-15, 15); }
										if (strlen($rex[1]) > 15) { $rex[1] = substr($rex[1], 0, 15); }
										$message .= "[$replace_counter] Replace : ".$rex[0]."<strong>".$rep[$i][1]."</strong>".$rex[1]."\n<br>";
									}
								} elseif ((false === $pos1) || (false === $pos2)) {
									$exit = true;
								}
							}
						} else {
							$string = str_replace($rep[$i][0], $rep[$i][1], $string);
							if ($result != 0) {
								$replace_counter++;
								if (strpos($rep[$i][0], $rep[$i][1])) { $message .= "[$replace_counter] Replace : ".$rep[$i][0]." TO ".$rep[$i][1]."\n<br>"; }
							}
						}
					}
					$fp = fopen($path, "w");
					fwrite($fp, $string);
					fclose($fp);
					if ($result != 0) { return $message; } else { return true; }
				}
			} else { return false; }
		} else { return false; }
	}
}
?>